package mya.mes.mdc.model.tag;

import com.alibaba.fastjson.JSONObject;
import mya.mes.mdc.model.excel.ExcelTagAlarmDef;
import mya.mes.mdc.model.tag.convert.AlarmTagConverter;
import mya.mes.mdc.model.tag.util.TagHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AlarmTag {


  /**
   * 在MDC中重新定义的逻辑TAG,和 PCL中的TAG对应
   */
  public String mdcTag;
  public int tagIndex;

  public String tagPrefix;

  /**
   * 发送公众号消息
   */
  public Boolean msgNotice;

  public ViewTag viewTag;

  /**
   * 定义长度小于32位
   */
  private transient ExcelTagAlarmDef[] tagAlertDefArray = new ExcelTagAlarmDef[MdcConstants.ALARM_TAG_BIT_MAX];


  private List<ExcelTagAlarmDef> excelTagAlarmRowList;


  public AlarmTag() {
  }

  public AlarmTag(Map json) {
    AlarmTagConverter.fromJson(json, this);
  }

  public JSONObject toJson() {
    JSONObject json = new JSONObject();
    AlarmTagConverter.toJson(this, json);
    return json;
  }


  public String getMdcTag() {
    return mdcTag;
  }

  public void setMdcTag(String mdcTag) {
    this.mdcTag = mdcTag;
  }

  public int getTagIndex() {
    return tagIndex;
  }

  public void setTagIndex(int tagIndex) {
    this.tagIndex = tagIndex;
  }

  public String getTagPrefix() {
    return tagPrefix;
  }

  public void setTagPrefix(String tagPrefix) {
    this.tagPrefix = tagPrefix;
  }


  public Boolean getMsgNotice() {
    return msgNotice;
  }

  public void setMsgNotice(Boolean msgNotice) {
    this.msgNotice = msgNotice;
  }

  public List<ExcelTagAlarmDef> getExcelTagAlarmRowList() {
    return excelTagAlarmRowList;
  }

  public void setExcelTagAlarmRowList(List<ExcelTagAlarmDef> excelTagAlarmRowList) {
    if (excelTagAlarmRowList == null) {
      this.excelTagAlarmRowList = null;
      Arrays.fill(tagAlertDefArray, null);
      return;
    }

    List<ExcelTagAlarmDef> list = excelTagAlarmRowList.stream().filter(def -> {
      return def != null;
    }).collect(Collectors.toList());
    ExcelTagAlarmDef[] arrayTmp = TagHelper.listToArray(list);
    System.arraycopy(arrayTmp, 0, tagAlertDefArray, 0, tagAlertDefArray.length);
    this.excelTagAlarmRowList = list;//Arrays.asList(arrayTmp);
  }

  public ExcelTagAlarmDef getByIndex(int index) {
    if (tagAlertDefArray == null || tagAlertDefArray.length == 0) {
      return null;
    }
    if (index < 0 || index >= tagAlertDefArray.length) {
      return null;
    }
    return tagAlertDefArray[index];
  }
}
